package com.example.gray.controller;

import com.example.gray.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Gray 2022/1/6-14:26
 */
@RestController
@RequestMapping(value = "mail")
@Api(tags = "邮件")
public class MailController {
    @Autowired
    JavaMailSender sender;

    @PostMapping(value = "/send")
    @ApiOperation(value = "发送一封邮件")
    public Object sendMail(String address,String text){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("来自服务器发送的邮件");
        message.setFrom("1015118305@qq.com");
        message.setTo(address);
        message.setText(text);
        try {
            sender.send(message);
            return Result.success("邮件发送成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(4002,"邮件发送失败","");
        }
    }
}
